import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export interface Product {
  id: number
  title: string
  price: number
  image: string
  tag?: string
  categoryId: number
  description?: string
  stock?: number
}

export interface Category {
  id: number
  name: string
  icon: string
  children?: SubCategory[]
}

export interface SubCategory {
  id: number
  name: string
  parentId: number
}

export const useProductStore = defineStore('product', () => {
  // 商品列表
  const products = ref<Product[]>([
    {
      id: 1,
      title: 'iPhone 15 Pro Max',
      price: 9999,
      image: 'https://images.unsplash.com/photo-1592750475338-74b7b21085ab?w=300&h=300&fit=crop',
      tag: '热销',
      categoryId: 1,
      description: '全新iPhone 15 Pro Max，搭载A17 Pro芯片，钛金属设计',
      stock: 50
    },
    {
      id: 2,
      title: 'MacBook Pro 16英寸',
      price: 19999,
      image: 'https://images.unsplash.com/photo-1541807084-5c52b6b3adef?w=300&h=300&fit=crop',
      tag: '新品',
      categoryId: 2,
      description: 'MacBook Pro 16英寸，M3 Max芯片，专业级性能',
      stock: 30
    },
    {
      id: 3,
      title: 'AirPods Pro 2',
      price: 1899,
      image: 'https://images.unsplash.com/photo-1606220945770-b5b6c2c55bf1?w=300&h=300&fit=crop',
      tag: '推荐',
      categoryId: 3,
      description: 'AirPods Pro 第二代，主动降噪，空间音频',
      stock: 100
    },
    {
      id: 4,
      title: 'iPad Air 5',
      price: 4399,
      image: 'https://images.unsplash.com/photo-1544244015-0df4b3ffc6b0?w=300&h=300&fit=crop',
      tag: '热销',
      categoryId: 4,
      description: 'iPad Air 第五代，M1芯片，10.9英寸液视网膜显示屏',
      stock: 80
    },
    {
      id: 5,
      title: 'Apple Watch Series 9',
      price: 2999,
      image: 'https://images.unsplash.com/photo-1434493789847-2f02dc6ca35d?w=300&h=300&fit=crop',
      tag: '新品',
      categoryId: 5,
      description: 'Apple Watch Series 9，S9芯片，双指互点手势',
      stock: 60
    },
    {
      id: 6,
      title: 'Canon EOS R5',
      price: 25999,
      image: 'https://images.unsplash.com/photo-1502920917128-1aa500764cbd?w=300&h=300&fit=crop',
      tag: '专业',
      categoryId: 6,
      description: 'Canon EOS R5 全画幅无反相机，4500万像素',
      stock: 20
    }
  ])

  // 分类列表
  const categories = ref<Category[]>([
    {
      id: 1,
      name: '手机',
      icon: 'phone-o',
      children: [
        { id: 11, name: 'iPhone', parentId: 1 },
        { id: 12, name: 'Android', parentId: 1 },
        { id: 13, name: '配件', parentId: 1 }
      ]
    },
    {
      id: 2,
      name: '电脑',
      icon: 'desktop-o',
      children: [
        { id: 21, name: 'MacBook', parentId: 2 },
        { id: 22, name: 'Windows笔记本', parentId: 2 },
        { id: 23, name: '台式机', parentId: 2 }
      ]
    },
    {
      id: 3,
      name: '耳机',
      icon: 'music-o',
      children: [
        { id: 31, name: '无线耳机', parentId: 3 },
        { id: 32, name: '有线耳机', parentId: 3 },
        { id: 33, name: '头戴式', parentId: 3 }
      ]
    },
    {
      id: 4,
      name: '平板',
      icon: 'tablet-o',
      children: [
        { id: 41, name: 'iPad', parentId: 4 },
        { id: 42, name: 'Android平板', parentId: 4 },
        { id: 43, name: '配件', parentId: 4 }
      ]
    },
    {
      id: 5,
      name: '手表',
      icon: 'clock-o',
      children: [
        { id: 51, name: 'Apple Watch', parentId: 5 },
        { id: 52, name: '智能手表', parentId: 5 },
        { id: 53, name: '运动手表', parentId: 5 }
      ]
    },
    {
      id: 6,
      name: '相机',
      icon: 'photo-o',
      children: [
        { id: 61, name: '单反相机', parentId: 6 },
        { id: 62, name: '无反相机', parentId: 6 },
        { id: 63, name: '镜头', parentId: 6 }
      ]
    },
    {
      id: 7,
      name: '游戏',
      icon: 'play-circle-o',
      children: [
        { id: 71, name: '游戏主机', parentId: 7 },
        { id: 72, name: '游戏配件', parentId: 7 },
        { id: 73, name: '游戏软件', parentId: 7 }
      ]
    },
    {
      id: 8,
      name: '更多',
      icon: 'ellipsis',
      children: [
        { id: 81, name: '智能家居', parentId: 8 },
        { id: 82, name: '数码配件', parentId: 8 },
        { id: 83, name: '其他', parentId: 8 }
      ]
    }
  ])

  // 轮播图数据
  const banners = ref([
    'https://images.unsplash.com/photo-1607082348824-0a96f2a4b9da?w=800&h=300&fit=crop',
    'https://images.unsplash.com/photo-1560472354-b33ff0c44a43?w=800&h=300&fit=crop',
    'https://images.unsplash.com/photo-1526170375885-4d8ecf77b99f?w=800&h=300&fit=crop'
  ])

  // 计算属性
  const hotProducts = computed(() => 
    products.value.filter(product => product.tag === '热销')
  )

  const newProducts = computed(() => 
    products.value.filter(product => product.tag === '新品')
  )

  // 方法
  const getProductById = (id: number) => {
    return products.value.find(product => product.id === id)
  }

  const getProductsByCategory = (categoryId: number) => {
    return products.value.filter(product => product.categoryId === categoryId)
  }

  const getCategoryById = (id: number) => {
    return categories.value.find(category => category.id === id)
  }

  return {
    products,
    categories,
    banners,
    hotProducts,
    newProducts,
    getProductById,
    getProductsByCategory,
    getCategoryById
  }
})